package com.manager.system.controller;
import com.manager.common.annotation.AspectLogInfo;
import com.manager.common.constant.OperationLogEnumType;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.office.sax.ExcelFactory;
import com.manager.common.office.sax.SXSSFExportExecutor;
import com.manager.common.utils.LargeFileHttpUploadUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.Operation_Log;
import com.manager.system.service.OperateLogService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.*;

/**
 * 
* <p>Title: OperationLogAction</p>
* <p>Description: 操作日期控制层，负责转发调用给业务处理层，并且把结果返回给前台</p>
* @author z.q.k
* @date 上午10:35:18
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/operationLog")
public class OperationController  {

	@Autowired
	private OperateLogService operateLogServiceImp;
	
	/**
	 * 查询操作日志，封装分页
	 * @param model
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getOperationLogList")
	public PageVO getOperationLogList(Operation_Log model, HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map<String, Object> map = new HashMap<String, Object>(); 
		JsonConfig jsonConfig = new JsonConfig();
		String roleTag=request.getParameter("roleTag");
		try {
			model.setTemp3(roleTag);
			PageVO pageVO = operateLogServiceImp.getOperateLists(model);
			return pageVO;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return new PageVO();
	}
	
	/**
	 * 删除操作日志
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/deleteOperationLog")
	public ResultVO deleteOperationLog(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			JSONArray jsonArray = JSONArray.fromObject(request.getParameter("data"));
			List<Operation_Log> list = new ArrayList<Operation_Log>();
			if(null != jsonArray && jsonArray.size() > 0) {
				for(int i=0; i< jsonArray.size(); i++) {
					 JSONObject jsonObject2 = JSONObject.fromObject(jsonArray.get(i));
					 Operation_Log model = (Operation_Log)JSONObject.toBean(jsonObject2, Operation_Log.class);
					 list.add(model);
				}
			}
			operateLogServiceImp.deleteOperateLogs((list));
			return ResultVO.success();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return ResultVO.failed();
	}
	
	/**
	 * 导出日志Excel数据
	 * @param model
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/exportExcelDatas")
	@AspectLogInfo(tableName="Operation_Log",content="importExcelDatas:导出日志",type= AspectMethodOperationEnum.DOWNLOAD, logType = OperationLogEnumType.DICTIONARY)
	public void exportExcelDatas(Operation_Log model, HttpServletRequest request, HttpServletResponse response) throws IOException{
	
		List<Object> listDatas = operateLogServiceImp.getExportData(model);
		try{
			String path = LargeFileHttpUploadUtils.getUploadFileSavePath() + "/" + UUID.randomUUID().toString().replaceAll("-","") +  ".xslx";
			File file = new File(path);
			FileOutputStream fos = new FileOutputStream(file);
			ExcelFactory.getExcelFactory().getExportExecutor().head(Operation_Log.class).sheet("日志").data(listDatas).write(fos);
			String downloadPath = path.substring(LargeFileHttpUploadUtils.getBasePath().length(), path.length())
					.replaceAll("\\\\", "/");

			response.setContentType("application/force-download");
			response.addHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode("日志数据", "UTF-8")+".xlsx" );

			byte[] buffer = new byte[1024];
			FileInputStream fis = null;
			BufferedInputStream bis = null;
			try {
				fis = new FileInputStream(file);
				bis = new BufferedInputStream(fis);
				OutputStream os = response.getOutputStream();
				int i = bis.read(buffer);
				while (i != -1) {
					os.write(buffer, 0, i);
					i = bis.read(buffer);
				}
			} finally {
				if (bis != null) {
					bis.close();
				}
				if (fis != null) {
					fis.close();
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
